package com.twu.biblioteca.operation;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.service.BookService;

import java.util.List;

import static com.twu.biblioteca.model.Status.available;
import static java.lang.String.format;

public class ListBooksOperation extends Operation {
    private BookService bookService;

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @Override
    public String operate(String[] args) {
        List<Book> books = bookService.getAllBooks();
        String result = "";
        for (Book book : books) {
            if (book.getStatus().equals(available)) {

                result += format("|%s|%s|%s|%s",
                        book.getBookId(),
                        book.getBookName(),
                        book.getAuthor(),
                        book.getYearPublished()) + "\r\n";
            }
        }
        return result;

    }
}
